package _spring_test;

import _spring_test._context.ContextTest;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.xml.ResourceEntityResolver;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @Author: huangya
 * @Date: 2019/5/30
 * @Description:
 */
public class Test {

    public static void main(String[] args) {

//        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//        User user = (User) ac.getBean("user");
//        System.out.println(111);

//        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext2.xml", User.class);
        System.setProperty("spring.profiles.default", "test");
        ApplicationContext ac = new ClassPathXmlApplicationContext("xml/application*.xml");
//        ApplicationContext ac = new ClassPathXmlApplicationContext("${user.country}.xml"){
        User user = (User) ac.getBean("user");
        System.out.println(user.getUserName());
        System.out.println(user.getDepartment().getDepartmentName());
        System.out.println(user.getRoles().iterator().next().getRoleName());

//        ContextTest contextTest = (ContextTest) ac.getBean("contextTest111");
//        System.out.println(contextTest.getCtId());
//
//        RedisTemplate redisTemplate = (RedisTemplate) ac.getBean("redisTemplate");
//        boolean aaa = redisTemplate.hasKey("aaa");
//        System.out.println(aaa);

//        FileSystemXmlApplicationContext fa = new FileSystemXmlApplicationContext("D:\\git_idea_workspace\\23DesignPattern\\src\\main\\resources\\applicationContext.xml");
//        fa.setValidating(false);
//        User user2 = (User) fa.getBean("user");
//        System.out.println(user2.getUserName());

//        /**
//         * 无法解析${}
//         */
//        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
//        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
////        reader.loadBeanDefinitions(new ClassPathResource("applicationContext.xml"));
//        reader.loadBeanDefinitions(new FileSystemResource("D:\\git_idea_workspace\\23DesignPattern\\src\\main\\resources\\applicationContext.xml"));
////        ResourceLoader resourceLoader = new PathMatchingResourcePatternResolver();
////        reader.setResourceLoader(resourceLoader);
////        reader.setEntityResolver(new ResourceEntityResolver(resourceLoader));
//        User user3 = (User) beanFactory.getBean("user");
//        System.out.println(user3.getUserName());

        /**
         * 无法解析${}
         */
//        DefaultListableBeanFactory beanFactory4 = new DefaultListableBeanFactory();
//        XmlBeanDefinitionReader reader4 = new XmlBeanDefinitionReader(beanFactory4);
//        reader4.loadBeanDefinitions("D:\\git_idea_workspace\\23DesignPattern\\src\\main\\resources\\applicationContext.xml");
//        User user4 = (User) beanFactory4.getBean("user");
//        System.out.println(user4.getUserName());


//        GenericBeanDefinition item = new GenericBeanDefinition();
//        item.setBeanClass(User.class);
//        item.getPropertyValues().add("name","test");
//        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//        applicationContext.registerBeanDefinition("item",item);
//        applicationContext.refresh();
//        User bean= (User) applicationContext.getBean("item");
//        System.out.println(bean.getUserName());


    }

}
